<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
    </style>
</head>

<body>
    <canvas id="canvas"></canvas>
</body>
<script id="TextureVS" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    uniform mat4 u_ModelM4;
    uniform mat4 u_PvM4;
    void main(){
        gl_Position = u_PvM4 * u_ModelM4 * a_Position;
    }
</script>
<script id="TextureFS" type="x-shader/x-fragment">
    precision mediump float;
    void main(){
        gl_FragColor = vec4(gl_FragCoord.x / 1000.0, gl_FragCoord.y / 1000.0, 1.0, 1.0);
    }
</script>
<script id="ColorVertexShader" type="x-shader/x-vertex">
    attribute vec4 a_Position;
    uniform mat4 u_ModelM4;
    uniform mat4 u_PvM4;
    varying vec4 v_ClipposLight;
    void main(){
        gl_Position = u_PvM4 * u_ModelM4 * a_Position;
        v_ClipposLight = u_PvM4 * u_ModelM4 * a_Position;
    }
</script>
<script id="ColorFragmentShader" type="x-shader/x-fragment">
    precision mediump float;
    uniform sampler2D u_ShadowMap;
    varying vec4 v_ClipposLight;
    void main(){
        vec3 fragPos = (v_ClipposLight.xyz / v_ClipposLight.w) / 2.0 + 0.5;
        vec4 shadowFrag = texture2D(u_ShadowMap, fragPos.xy);
        gl_FragColor = shadowFrag;
    }
</script>
<script type="module">
    import { Matrix4, Vector3, Color, OrthographicCamera, PerspectiveCamera, Spherical } from '../js/three.module.js';
    import { CreateProgram, imgPromise } from '../js/Utils.js';
    import lib from '../js/ThreeLib.js';
    import CameraController from '../js/CameraController.js'
    import Poly from '../js/Poly4.js'
    import Control from '../js/Control.js'
    import Square from '../js/objs/Square.js'

    import Geo from '../js/Poly2/Geo.js'
    import Mat from '../js/Poly2/Mat.js'
    import Obj3D from '../js/Poly2/Obj3D.js'
    import Scene from '../js/Poly2/Scene.js'
    import Frame from '../js/Poly2/Frame.js'



    //#region gl
    const canvas = document.getElementById("canvas");
    canvas.width = window.innerWidth;
    canvas.height = window.innerHeight - 4;

    const gl = canvas.getContext("webgl", { antialias: true, });

    gl.enable(gl.BLEND);
    gl.blendFunc(gl.SRC_ALPHA, gl.ONE_MINUS_SRC_ALPHA);
    gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);//去图片描边
    gl.enable(gl.DEPTH_TEST);
    //#endregion

    const scene = new Scene({ gl: gl });
    scene.addAxisProgram();
    scene.regProgram('color', {
        program: CreateProgram(gl,
            document.querySelector('#ColorVertexShader').innerText,
            document.querySelector('#ColorFragmentShader').innerText),
        attribs: ['a_Position'],
        uniforms: ['u_PvM4', 'u_ModelM4', 'u_ShadowMap']
    });

    const frame = new Frame({ gl });
    frame.regProgram('texture', {
        program: CreateProgram(gl,
            document.querySelector('#TextureVS').innerText,
            document.querySelector('#TextureFS').innerText),
        attribs: ['a_Position'],
        uniforms: ['u_PvM4', 'u_ModelM4']
    });

    const pvM4 = new Matrix4();
    const modelM4 = new Matrix4();
    const eye = new Vector3(1.2, 1.2, 1.2);
    const lookAt = new Vector3();

    const camera = new PerspectiveCamera(45, canvas.width / canvas.height, 1, 20);
    const cameraController = new CameraController({
        canvas,
        camera,
        pvMatrix: pvM4,
        eye,
        lookAt,
    });


    const axis = new Obj3D().createAxis('axis');
    scene.add(axis);

    const square = new Square(0.5);
    const [squareVertices, squareElements, squareNormals] = [
        square.vertices, square.elements, square.normals
    ];


    let squareTexture = new Obj3D({
        geo: new Geo({
            data: {
                'a_Position': { size: 3, array: new Float32Array(squareVertices) },
            },
            element: {
                array: new Uint16Array(squareElements)
            }
        }),
        mat: new Mat({
            program: 'texture',
            mode: ['TRIANGLES'],
            data: {
                'u_PvM4': { type: 'uniformMatrix4fv', value: pvM4.elements },
                'u_ModelM4': { type: 'uniformMatrix4fv', value: modelM4.elements },
            },
        }),
    });
    frame.add(squareTexture);
    frame.draw();

    let square3D = new Obj3D({
        geo: new Geo({
            data: {
                'a_Position': { size: 3, array: new Float32Array(squareVertices) },
            },
            element: {
                array: new Uint16Array(squareElements)
            }
        }),
        mat: new Mat({
            program: 'color',
            mode: ['TRIANGLES'],
            data: {
                'u_PvM4': { type: 'uniformMatrix4fv', value: pvM4.elements },
                'u_ModelM4': { type: 'uniformMatrix4fv', value: modelM4.elements },
            },
            maps: { 'u_ShadowMap': { texture: frame.texture } }
        }),
    });
    scene.add(square3D);


    (function ani() {
        draw();
        requestAnimationFrame(ani)
    })();


    function draw() {
        scene.setUniform('u_PvM4', { value: pvM4.elements });

        frame.draw();
        scene.draw();
    }
</script>

</html>